package com.example.administrator.printoutindemo.mvp.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.di.component.DaggerDaiLingLiaoComponent;
import com.example.administrator.printoutindemo.di.module.DaiLingLiaoModule;
import com.example.administrator.printoutindemo.mvp.contract.DaiLingLiaoContract;
import com.example.administrator.printoutindemo.mvp.presenter.DaiLingLiaoPresenter;
import com.example.administrator.printoutindemo.mvp.ui.adapter.GetOutstorageCheckWaitingApproveAdapter;
import com.example.administrator.printoutindemo.mvp.ui.entity.DaiLingLiao;
import com.example.administrator.printoutindemo.mvp.ui.entity.GetOutstorageCheckWaitingApproveEntity;
import com.example.administrator.printoutindemo.mvp.ui.entity.InventoryAndroid;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.List;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_DAI_LING_LIAO)
public class DaiLingLiaoActivity extends BaseActivity<DaiLingLiaoPresenter> implements DaiLingLiaoContract.View {

    private static final int LOGINSUCCESS = 0;
    private static final int NETWORKERROR = 2;
    private static final int SYSERROR = 3;
    private static final String WEBSERVICE_CONFIG = "TEXT";
    View mEmptyLayout;
    TextView empty_text;
    ImageView empty_image;
    private ListView store_sub_list_listView = null;
    private GetOutstorageCheckWaitingApproveAdapter adapter = null;
    private GetOutstorageCheckWaitingApproveEntity oli = null;
    private List<GetOutstorageCheckWaitingApproveEntity> li_oli = null;
    private SharedPreferences sPreferencces;
    private String SYSException = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerDaiLingLiaoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .daiLingLiaoModule(new DaiLingLiaoModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_dai_shen_pi; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_dai_ling_liao));
//        getDaiLingLiaoData();
        initEmpty();
        sPreferencces = getSharedPreferences(WEBSERVICE_CONFIG, 0);


        store_sub_list_listView = (ListView) findViewById(R.id.store_sub_list_listView);

        adapter = new GetOutstorageCheckWaitingApproveAdapter(this);
        oli = new GetOutstorageCheckWaitingApproveEntity();
        String user_id = sPreferencces.getString(BaseConstants.UID, "");
        getOutstorageCheckWaitingOutStore(user_id);


    }

    private void initEmpty() {
        mEmptyLayout = (View) findViewById(R.id.empty);
        empty_text = (TextView) findViewById(R.id.empty_text);
        empty_image = (ImageView) findViewById(R.id.empty_image);

        empty_image.setImageResource(com.xiaoxing.zhinenghuacangku.R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#CFCFCF"));
        empty_text.setText("暂无数据");
    }

    private void getOutstorageCheckWaitingOutStore(String user_id) {

        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);

        store_sub_list_listView.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.VISIBLE);

        String methodName = "GetOutstorageCheckWaitingOutStore";

        SoapParams params = new SoapParams();
        params.put("user_id", user_id);

        /**
         * 请求
         */
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (li_oli != null) li_oli.clear();
                    li_oli = new Gson().fromJson(result, new TypeToken<List<GetOutstorageCheckWaitingApproveEntity>>() {}.getType());
//                    li_oli = oli.getListFromJSON(result);
                    ;
                    adapter.setList(li_oli);
                    store_sub_list_listView.setAdapter(adapter);
                    mEmptyLayout.setVisibility(View.GONE);
                    store_sub_list_listView.setVisibility(View.VISIBLE);
                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                    store_sub_list_listView.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }


    private void getDaiLingLiaoData() {
        mPresenter.getDaiLingLiaoData();
    }

    @Override
    public void getDaiLingLiaoDataSuccess(DaiLingLiao entityData) {


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
